﻿$(function(){//延迟加载  
	var ipdeployId='';
	var oltip='';
	var oltname='';
	var oltponnum=0;
	var ylpon=0;
	var boardnum=0;
	var dvfirm='';
	var oltId='';
	var pppvlanbegin='';
	var pppvlanend='';
	$('#newolt').datagrid('mergeCells',{
		index:6,
		field:'2',
		colspan:4
	});
	$('#newolt').datagrid('mergeCells',{
		index:2,
		field:'2',
		colspan:5
	});
	$('#newolt').datagrid('mergeCells',{
		index:3,
		field:'2',
		colspan:5
	});
	$('#oltname').validatebox({
		required:true
	});
	$('#oltponnum').numberbox({
		required:true
	});
	$('#ylpon').numberbox({
		prompt:'预留..',
		min:2,
		max:30,
		required:true
	});
	$('#boardnum').numberbox({
		required:true
	});
	$('#pppvlanbegin').numberbox({
		prompt:'PPPOE VLAN起始...',
		required:true
	});
	$('#pppvlanend').numberbox({
		prompt:'PPPOE VLAN终止...',
		required:true,
		buttonText:'生成',
		buttonIcon:'icon-search',
		onClickButton:function(){
			if($(this).val()==''||$('#pppvlanbegin').val()==''||$(this).val()<=$('#pppvlanbegin').val()){
				alert('不合法');
			}else{
				$.ajax({
					url:'getoltvlans?oltpppvlan='+($('#pppvlanbegin').val()+'-'+$(this).val()),
					success:function(data){
						$('#iptvvlan').val(data.iptvvlan);
						$('#ylvlan').val(data.ylvlan);
						$('#voipvlan').val(data.voipvlan);
					}
				});
			}
		}
	});
	$('#advlan').numberbox({
		required:true
	});
	$('#olttype').combobox();
	$('#oltpontype').combobox();
	
	$('#jfl').load('showjifangsec',function(){
		$.getScript('/xasj/js/component/jifangsec.js');
	});
	
	$('#shiyan').load('showjifangsec',function(){
		$.getScript('/xasj/js/component/jifangsec.js');
	});
	
	$('#xinghao').load('dvfirmsec',function(){
		$.getScript('/xasj/js/component/dvmodelsec.js');
	});
	
	$('#ceshi').click(function(){
		alert($('#jifang').combobox('getValue'));
	});
	
	$('#requestipnum').numberbox({
		prompt:'请求数量',
		min:3,
		//max:20,
		required:true
	});
	
	$('#getoltip').textbox({
		buttonText:'获取IP',
		buttonIcon:'icon-search',
		prompt:'自动获取...',
		onClickButton:function(){
			if($('#jifang').combobox('getValue')=='请选择'||$('#requestipnum').val()==''){
				alert('请选择机房和需求的IP数')
			}else{
				//alert($('#jifang').combobox('getValue')+$('#requestipnum').val());
				var url='requestip?jfId='+$('#jifang').combobox('getValue')
				+'&requestipnum='+$('#requestipnum').val()+'&dvbelong=oltip'+'&djxId='
				+$('#dajuxiang').combobox('getValue');
				//alert(url);
				$.ajax({					
					url:'requestip?jfId='+$('#jifang').combobox('getValue')
					+'&requestipnum='+$('#requestipnum').val()+'&dvbelong=oltip'+'&djxId='
					+$('#dajuxiang').combobox('getValue'),
					type:'get',
					dataType:'json',
					success:function(data){
						//alert(data.iprange);
						ipdeployId=data.ipdeployId;
						oltip=$('#oltip').val();
						$('#getoltip').textbox('setValue',data.dvip);
						$('#getgw').val(data.dvgateway);
						$('#iprange').val(data.iprange);
						alert(data.msg);
					}
				})
			}
		}
	});
	$('#voipnum').numberbox({
		prompt:'请求数量',
		min:3,
		//max:20,
		required:true
	});
	$('#getoltvoip').textbox({
		buttonText:'获取IP',
		buttonIcon:'icon-search',
		prompt:'自动获取...',
		onClickButton:function(){
			if($('#jifang').combobox('getValue')=='请选择'||$('#voipnum').val()==''){
				alert('请选择机房和需求的IP数')
			}else{
				//alert($('#jifang').combobox('getValue')+$('#requestipnum').val());
				var url='requestip?jfId='+$('#jifang').combobox('getValue')
				+'&requestipnum='+$('#voipnum').val()+'&dvbelong=oltvoip'+'&djxId='
				+$('#dajuxiang').combobox('getValue');
				//alert(url);
				$.ajax({					
					url:url,
					type:'get',
					dataType:'json',
					success:function(data){
						//alert(data.iprange);
						ipdeployId=data.ipdeployId;
						oltip=$('#oltip').val();
						$('#getoltip').textbox('setValue',data.dvip);
						$('#voipgw').val(data.dvgateway);
						$('#voiprange').val(data.iprange);
						alert(data.msg);
					}
				})
			}
		}
	});
	$('#step1').linkbutton({
		text:'下一步',
	    iconCls: 'icon-ok',
	    onClick:function(){
	    	oltname=$('#oltname').val();
	    	$('#addparent').load('todvparent',{
	    		dvbelong:'olt',
	    		dvip:$('#getoltip').val(),
	    		dvname:oltname,
	    		dvId:'69c998d1-af4c-4a9e-a110-7da9da7dc3c3'
	    	},function(){
	    		$('#aa').accordion('select','设备上联');
	    		$.getScript('/xasj/js/component/dvparent.js');
	    		$('#dg').after('<a id="pre1" style="width:80px"></a><a id="step2" style="width:100px"></a>');
	    		$('#pre1').linkbutton({
	    			text:'上一步',
	    		    onClick:function(){
	    		    	$('#aa').accordion('select','添加OLT设备');
	    		    }
	    		});
	    		$('#step2').linkbutton({
	    			text:'下一步',
	    			iconCls:'icon-ok',
	    			onClick:function(){
	    				oltponnum=$('#oltponnum').val();
	    				ylpon=$('#ylpon').val();
	    				boardnum=$('#boardnum').val();
	    				dvfirm=$('#dvfirm').combobox('getValue');
	    				oltId=$('#dvId').val();
	    				oltip=$('#oltip').val();
	    				pppvlanbegin=$('#pppvlanbegin').val();
	    				pppvlanend=$('#pppvlanend').val();
	    				oltname=$('#oltname').val();
	    				$('#newonu').datagrid({
	    					iconCls: 'icon-edit',
	    					singleSelect: true,
	    					url:'shownewonu',
	    					onClickRow: onClickonuRow,
	    					rownumbers:true,
	    					queryParams:{
	    						oltponnum:oltponnum,
	    						ylpon:ylpon,
	    						boardnum:boardnum,
	    						dvfirm:dvfirm,
	    						oltId:oltId,
	    						oltip:oltip,
	    						pppvlanbegin:pppvlanbegin,
	    						pppvlanend:pppvlanend,
	    						oltname:oltname
	    					},
	    					toolbar: [{
	    						iconCls: 'icon-save',
	    						handler: function(){
	    							acceptonu();
	    							}
	    						},
	    						'-'
	    						,{
	    						iconCls: 'icon-remove',
	    						handler: function(){
	    							rejectonu();
	    							}
	    						}],
	    					columns:[[{
	    						field:'onuaddress',width:100,title:'地址',editor:{
	    							type:'combobox',
	    							options:{
	    								valueField:'addressvalue',
	    								textField:'addressname',
	    								data:[{
	    									addressvalue:'未分配',
	    									addressname:'未分配'
	    								},{
	    									addressvalue:'预留',
	    									addressname:'预留'
	    								}]
	    							}
	    						}
	    					},{
	    						field:'onuepbaloc',width:70,title:'EPBA板',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'onupon',width:60,title:'PON口',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'onunum',width:80,title:'ONU编号',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'onupppv',width:90,title:'pppoe vlan',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'iptvvlan',width:90,title:'iptv vlan',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'voipvlan',width:90,title:'voip vlan',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'ylvlan',width:90,title:'预留 vlan',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'pondetail',width:70,title:'ONU PON',editor:{
	    							editable:false
	    						}
	    					},{
	    						field:'onuodf',width:70,title:'ONU ODF',editor:{
	    							editable:false
	    						}
	    					}]],
	    					onLoadSuccess:function(){
	    						$('#aa').accordion('select','配置ONU');
	    						//$('#newonu').after('<a id="step3" style="width:100px"></a>');
	    						$(this).datagrid('freezeRow',0);
	    						$('#compl').show();
	    					}
	    				});
	    			}
	    		});
	    	});
	    }
	});
	$('#step3').linkbutton({
		text:'完成',
	    iconCls: 'icon-ok',
	    onClick:function(){
	    	alert('OK')
	    }
	});
	$('#pre2').linkbutton({
		text:'上一步',
	    onClick:function(){
	    	$('#aa').accordion('select','设备上联');
	    }
	});
	$('#compl').hide();
	var editIndex1 = undefined;	
	function endEditing1(){
		if (editIndex1 == undefined){return true}
		if ($('#newonu').datagrid('validateRow', editIndex1)){
			//var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'productid'});
			//var productname = $(ed.target).combobox('getText');
			//$('#dg').datagrid('getRows')[editIndex]['productname'] = productname;
			//$('#dg').datagrid('endEdit', editIndex);
			editIndex1 = undefined;
			return true;
		} else {
			return false;
		}
	}
	function onClickonuRow(index){
		if (editIndex1!= index){
			if (endEditing1()){
				$('#newonu').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				editIndex1 = index;
			} else {
				$('#newonu').datagrid('selectRow', editIndex1);
			}
		}
	}
	function removeitonu(){
		if (editIndex1 == undefined){return}
		$('#newonu').datagrid('cancelEdit', editIndex1)
				.datagrid('deleteRow', editIndex1);
		editIndex1 = undefined;
	}

	function acceptonu(){
		if (endEditing1()){
			$('#newonu').datagrid('acceptChanges');
		}
	}

	function rejectonu(){
		$('#newonu').datagrid('rejectChanges');
		editIndex1 = undefined;
	}
    $.extend($.fn.validatebox.defaults.rules, {    
    	 ip : {// 验证IP地址  
             validator : function(value) {  
                 var reg = /^((1?\d?\d|(2([0-4]\d|5[0-5])))\.){3}(1?\d?\d|(2([0-4]\d|5[0-5])))$/ ;  
                 return reg.test(value);  
             	},  
             message : 'IP地址格式不正确'  
    	 	}
    });
});